﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using 修为尽失.辅助.Resources;

namespace 修为尽失.辅助
{
    public static class ArrayHelper
    {
        public static List<Uri> ToUris(this string txt, string[] separators = null)
        {
            if (txt == null || separators == null)
                return null;
            var ss = txt.Split(separators, StringSplitOptions.RemoveEmptyEntries);
            if (ss.Length > 0)
            {
                try
                {
                    return ss.Select(a => new Uri(a, UriKind.Absolute)).ToList();
                }
                catch (Exception e)
                {
                    e.ShowException();
                }
            }

            return null;
        }

        public static List<Uri> ToUris(this string txt)
        {
            return txt.ToUris(ResManager.Strings.DBTextSeperator);
        }

        public static ObservableCollection<KeyValuePair<Uri, string>> ToUrisWithSpecification(this string txt)
        {
            var ss = txt.Split(ResManager.Strings.DBUriTextWithSpecSeperator, StringSplitOptions.RemoveEmptyEntries);
            if (ss.Length % 2 == 1)
            {
                // new Exception("参考链接格式错误！").ShowException();
                return null;
            }

            try
            {
                var list = new ObservableCollection<KeyValuePair<Uri, string>>();
                string spec = null;
                for (var i = 0; i < ss.Length; i++)
                {
                    var a = ss[i].Trim('\r', '\n');
                    if (i % 2 == 1)
                    {
                        var uri = new Uri(a, UriKind.Absolute);
                        list.Add(new KeyValuePair<Uri, string>(uri, spec));
                    }
                    else
                    {
                        spec = a;
                    }
                }

                return list;
            }
            catch (Exception e)
            {
                e.ShowException();
            }

            return null;
        }

        public static string ConvertToString(this ObservableCollection<KeyValuePair<Uri, string>> coll)
        {
            if (coll == null) return null;
            var sb=new StringBuilder();
            foreach (var c in coll)
            {
                sb.Append($"{c.Value??""}^:{c.Key}^;");
            }
            return sb.ToString();
        }

        public static ObservableCollection<string> ToStringList(this string txt)
        {
            var list = txt.Split(ResManager.Strings.DBTextSeperator, StringSplitOptions.RemoveEmptyEntries).ToList();
            return new ObservableCollection<string>(list);
        }

        public static string ConvertToString<T>(this ObservableCollection<T> coll,bool reservenull=false)
        {
            if (coll == null) return null;
            var sb=new StringBuilder();
            foreach (var c in coll)
            {
                if (c == null&&reservenull)
                {
                    sb.Append("^;");
                }
                else
                {
                    sb.Append($"{c}^;");
                }
            }

            return sb.ToString();
        }
        

        public static string ConcatToString<T>(this IEnumerable<T> a)
        {
            if (a == null) return null;
            var sb = new StringBuilder();
            foreach (var item in a)
            {
                sb.Append(item == null ? "" : item.ToString());
            }

            return sb.ToString();
        }
    }
}